<template>
  <div>
    地址管理
    <popup ref="editBrand">1</popup>
    <el-card style="margin:30px" shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button type="primary" @click="append">新增</el-button>

      </div>
      <!-- card body -->
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="#"
          fixed
        />
        <el-table-column
          prop="addressName"
          label="地址名称"
          width="180"
          align="center"
        />
        <el-table-column
          label="默认发货地址"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.sendStatus"
              :active-value="1"
              :inactive-value="0"
              @change="SwiDeliverChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="收货人姓名"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="收货人电话"
          align="center"
        />
        <el-table-column
          prop="address"
          label="收货人地址"
          align="center"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.province }}</div>
            <div>{{ scope.row.city }}</div>
            <div>{{ scope.row.region }}</div>
            <div>{{ scope.row.detailAddress }}</div>
          </template></el-table-column>
        <el-table-column
          label="默认收货地址"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.receiveStatus"
              :active-value="1"
              :inactive-value="0"
              @change="SwiTakeChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
        />
        <el-table-column
          prop="address"
          label="操作"
          align="center"
          fixed="right"
        />
      </el-table>
    </el-card>

  </div>
</template>

<script>
import { addressList, setReceiveOne, setSendOne } from '@/api/order/address/index'
import popup from './popup.vue'

export default {
  components: {
    popup
  },
  props: {

  },
  data() {
    return {
      list: []
    }
  },
  computed: {

  },
  watch: {

  },
  created() {
    this.init()
  },
  mounted() {

  },
  methods: {
    init() {
      addressList().then(res => {
        console.log(res)
        const { message: errMsg, success, data } = res
        if (success) {
          this.list = data.items
        } else {
          this.$message.error(errMsg)
        }
      })
    },
    append() { // 点击新增
      this.$refs.editBrand.isDialogFormVisible()
    },
    SwiTakeChange(val) { // 点击切换默认收货
      console.log(val)
      setReceiveOne({
        id: val.id,
        receiveStatus: val.receiveStatus
      }).then(res => {
        console.log(res)
        const { message: errMsg, success } = res
        if (success) {
          this.$message({
            message: '修改状态成功',
            type: 'success'
          })
        } else {
          this.$message.error(errMsg)
        }
        this.init()
      })
    },
    SwiDeliverChange(val) { // 点击默认发货
      console.log(val)
      setSendOne({
        id: val.id,
        sendStatus: val.sendStatus
      }).then(res => {
        console.log(res)
        const { message: errMsg, success } = res
        if (success) {
          this.$message({
            message: '修改状态成功',
            type: 'success'
          })
        } else {
          this.$message.error(errMsg)
        }
        this.init()
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
